package com.sky.controller.admin;

import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 山顶洞人郑某
 * @className CategoryController.java
 * @date 2024 01 19
 */
@RestController
@Slf4j
@RequestMapping("/admin/category")
@Api("分类管理模块")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @ApiOperation(value = "新增分类")
    @PostMapping
    public Result<String > add(@RequestBody Category category){
        log.info("新增分类信息：{}",category);
        categoryService.add(category);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @ApiOperation(value = "根据类型查询分类")
    @GetMapping("/list")
   public Result<List<Category>> getByType(Integer type){
        log.info("套餐类型为{}",type);
         List<Category> list =  categoryService.getByType(type);
         log.info("套餐信息{}",list);
         return Result.success(list);
   }
  @ApiOperation("根据id修改分类信息")
   @PutMapping
   public Result<String > updateById(@RequestBody Category category){
        log.info("修改分类信息为{}",category);
       categoryService.update(category);
       return Result.success();
   }

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
   @ApiOperation("分类分页查询")
   @GetMapping("/page")
   public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
       log.info("分页参数为{}",categoryPageQueryDTO);
     PageResult pageResult =  categoryService.page(categoryPageQueryDTO);
     return Result.success(pageResult);
   }

    /**
     * 分类启用和禁用
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("分类启用和禁用")
    @PostMapping("/status/{status}")
   public Result<String> setStatus(@PathVariable Integer status,Long id){
    log.info("停用启用分类id为{}",id);
    categoryService.setStatus(status,id);
    return Result.success();
   }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除分类")
    @DeleteMapping
    public Result<String> deleteById(Long id){
        log.info("删除分类id为{}",id);
        categoryService.deleteById(id);
        return Result.success();
    }




}
